package org.iryshka.skyserver;

import org.iryshka.logit.ConsoleFileLogging;
import org.iryshka.logit.ConsoleFileLogging.WritingTo;

import java.io.*;

public class ConfigAnalyze {
	
	public enum Listing{
		ON, 
		OFF;
	}
	public static File dirConfig;// CONFIGURATION DATA file
	private static String lineConfig;
	
	public static String ServerDirectory;
	public static Integer NumRequests;
	public static Integer Port;
	public static Listing ListingOnOff;

	public static void listOrNot (Listing listOrNot){
		ConfigAnalyze.ListingOnOff = listOrNot;
	}
	
	public void ConfigFileAnylize (String[] args) throws FileNotFoundException, IOException {
		
		if(args.length > 0){
			dirConfig = new File(args[0]);
		}
		else{
			dirConfig = new File("C:/temp/skywebserver.config.properties");
		}

		try (BufferedReader readingLineStream = new BufferedReader (new FileReader (dirConfig))){
			
			for (; (lineConfig = readingLineStream.readLine()) != null;){
				checkConfigName(lineConfig);
			}
		}
		
		if (Port != null && ServerDirectory != null && ListingOnOff != null && ConsoleFileLogging.destToWrite != null ){
			System.out.println("*********** Whole list of configurations has been successfully downloaded !********* \n");
		}
	}
	
	
	private void checkConfigName (String strToCheck){
		
		String[] subStrToCheck = strToCheck.split("=");
		
		if(subStrToCheck[0].contains("ServerDirectory")){
			if (!(subStrToCheck[1]).equals("")){
				ServerDirectory = subStrToCheck[1];
			}
		}
		else if(subStrToCheck[0].contains("NumRequests")){
			if(!(subStrToCheck[1]).equals("")){
				NumRequests = new Integer (subStrToCheck[1]);
				if (NumRequests < 2){
					System.out.println("Configuration data for NumRequests is incorrect! \n");
				}
			}
		}
		else if(subStrToCheck[0].contains("Port")){
			if(!(subStrToCheck[1]).equals("")){
				if (new Integer (subStrToCheck[1]) > 1024 && new Integer (subStrToCheck[1]) < 65536){
					Port = new Integer (subStrToCheck[1]);
				}
				else{
					System.out.println("Configuration data for port is incorrect! \n");
				}
			}
		}
		else if(subStrToCheck[0].contains("Listing")){
			if((subStrToCheck[1]).equals("ON")){
				ConfigAnalyze.listOrNot(Listing.ON);
			}
			else{
				ConfigAnalyze.listOrNot(Listing.OFF);
			}
		}
		else if(subStrToCheck[0].contains("LogTo")){
			if(!(subStrToCheck[1]).equals("")){
				ConsoleFileLogging.whereToWrite(ConsoleFileLogging.WritingTo.valueOf(subStrToCheck[1]));
			}
			else{
				System.out.println("Configuration data for logging is incorrect! \n");
			}
		}
		else if(subStrToCheck[0].contains("LogPath")){
			if(ConsoleFileLogging.destToWrite == WritingTo.FILE ||
				ConsoleFileLogging.destToWrite == WritingTo.FILE_CONSOLE){
				if (!(subStrToCheck[1]).equals("")){
					ConsoleFileLogging.setDirToWrite(subStrToCheck[1]);
				}
				else{
					System.out.println("Configuration data for LogPath is incorrect! \n");
				}
			}
		}
	}
}
